package com.mialab.servlet_demo;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/GetWebInitParamServlet")
public class GetWebInitParamServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public GetWebInitParamServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置响应到客户端的文本类型为HTML
		response.setContentType("text/html;charset=UTF-8");
		// 得到ServletContext对象
		ServletContext context = this.getServletContext();
		// 得到包含所有初始化参数名的Enumeration对象
		Enumeration<String> paramNames = context.getInitParameterNames();
		// 获取输出流
		PrintWriter out = response.getWriter();
		// 遍历所有的初始化参数名，得到相应的参数值，打印到控制台
		out.print("<h2>当前Web应用的所有初始化参数：</h2>");		
		// 遍历所有的初始化参数名，得到相应的参数值并打印
		while (paramNames.hasMoreElements()) {
			String name = paramNames.nextElement();
			String value = context.getInitParameter(name);
			out.println(name + "：" + value);
			out.println("<br>");
		}
		out.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
